package com.zsc.edu.gateway.modules.system.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zsc.edu.gateway.framework.mybatisplus.DataPermission;
import com.zsc.edu.gateway.modules.operationLog.entity.OperationLogAnnotation;
import com.zsc.edu.gateway.modules.system.dto.AuthorityCreateDto;
//import com.zsc.edu.gateway.modules.system.dto.RoleAuthCreateDto;
import com.zsc.edu.gateway.modules.system.dto.RoleDto;
import com.zsc.edu.gateway.modules.system.entity.Role;
import com.zsc.edu.gateway.modules.system.query.RoleQuery;
import com.zsc.edu.gateway.modules.system.service.RoleAuthService;
import com.zsc.edu.gateway.modules.system.service.RoleService;
import com.zsc.edu.gateway.modules.system.vo.RoleVo;
import lombok.AllArgsConstructor;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.Set;

/**
 * 角色Controller
 *
 * @author harry yao
 */
@AllArgsConstructor
@RestController
@RequestMapping("/api/rest/role")
public class RoleController {


    private final RoleService service;

    /**
     * 返回所有角色列表 hasAuthority('SYSTEM:ROLE:QUERY')
     *
     * @return 所有角色列表
     */
    @DataPermission
    @GetMapping
    @PreAuthorize("hasAuthority('system:role:query')")
    public Page<Role> query(RoleQuery query, Page<Role> page) {
        return service.page(page, query.wrapper());
    }

    /**
     * 新建角色 hasAuthority('SYSTEM:ROLE:CREATE')
     *
     * @param dto 表单数据
     * @return Role 新建的角色
     */
    @PostMapping
    @PreAuthorize("hasAuthority('system:role:create')")
    @OperationLogAnnotation(content = "'角色'", operationType = "新建")
    public Role create(@RequestBody RoleDto dto) {
        return service.create(dto);
    }

    /**
     * 更新角色 hasAuthority('SYSTEM:ROLE:UPDATE')
     *
     * @param dto 表单数据
     * @param id  ID
     * @return Role 更新后的角色
     */
    @PatchMapping("{id}")
    @PreAuthorize("hasAuthority('system:role:update')")
    @OperationLogAnnotation(content = "'角色'", operationType = "更新")
    public Role update(@RequestBody RoleDto dto, @PathVariable("id") Long id) {
        return service.edit(dto, id);
    }

    /**
     * 切换角色"启动/禁用"状态 hasAuthority('SYSTEM:ROLE:UPDATE')
     *
     * @param id ID
     * @return Role 更新后的角色
     */
    @PatchMapping("{id}/toggle")
    @PreAuthorize("hasAuthority('system:role:update')")
    @OperationLogAnnotation(content = "'角色状态'", operationType = "更新")
    public Boolean toggle(@PathVariable("id") Long id) {
        return service.toggle(id);
    }

    /**
     * 查询角色详情 hasAuthority('SYSTEM:ROLE:QUERY')
     *
     * @param id ID
     * @return Role 角色详情
     */
    @GetMapping("{id}")
    @PreAuthorize("hasAuthority('system:role:query')")
    public RoleVo detail(@PathVariable Long id) {
        return service.detail(id);
    }

    /**
     * 删除角色 hasAuthority('SYSTEM:ROLE:DELETE')
     *
     * @param id ID
     * @return Role 更新后的角色
     */
    @DeleteMapping("{id}")
    @PreAuthorize("hasAuthority('system:role:delete')")
    @OperationLogAnnotation(content = "'角色'", operationType = "删除")
    public Boolean delete(@PathVariable Long id) {
        return service.delete(id);
    }

}

